package com.hl.bean;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.hl.annotations.After;
import com.hl.annotations.Before;
import com.hl.interfaces.BeforeAfterInterface;
/**
 * 方法对象聚合在一起
 * @author helei
 *
 */
public class MethodInvokeBean {
	/**方法所依附的对象*/
	private Object obj;
	/**执行的方法*/
	private Method method;
	private BeforeAfterInterface b;
	private BeforeAfterInterface a;
	public MethodInvokeBean(){}
	public MethodInvokeBean(Object obj,Method method){
		this.method=method;
		this.obj=obj;
		Before b=(Before)this.method.getAnnotation(Before.class);
		After a=(After)this.method.getAnnotation(After.class);
		try {
			if(b!=null){
				this.b=(BeforeAfterInterface)b.value().newInstance();
			}
			if(a!=null){
				this.a=(BeforeAfterInterface)a.value().newInstance();
			}
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	/**执行方法*/
	public void invoke(Object... objs){
		try {
			if(b!=null){b.run();}
			this.method.invoke(obj, objs);
			if(a!=null){a.run();}
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
		}
	}
}
